package org.example.MyBatisPlus.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.example.MyBatisPlus.Mapper.BookMapper;
import org.example.MyBatisPlus.Service.IBookService;
import org.example.pojos.Book;
import org.example.pojos.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author : zhangbo
 * @data : 2024/7/30
 * @description :
 */
@Service
@Slf4j
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Autowired
    private BookMapper bookMapper;

    /**
     * @param str
     * @return
     */
    @Override
    public ResultResponse getBooks(String str) {
        if (Strings.isNullOrEmpty(str)) {
            return new ResultResponse(false, "模糊查询条件为空");
        }
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!Strings.isNullOrEmpty(str), Book::getName, str)
                .or().like(!Strings.isNullOrEmpty(str), Book::getType, str)
                .or().like(!Strings.isNullOrEmpty(str), Book::getDescription, str);
        List<Book> books = bookMapper.selectList(wrapper);
        books.stream().forEach(book -> {
            Book book1 = new Book();
            book1.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            bookMapper.updateById(book1);
        });
        return new ResultResponse(true, books);
    }

    /**
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Book> getPage(Integer page, Integer pageSize, Book book1) {
        IPage<Book> iPage = new Page(page, pageSize);
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        //如果条件成立,就加入该匹配条件
        wrapper.like(!Strings.isNullOrEmpty(book1.getName()), Book::getName, book1.getName())
                .or().like(!Strings.isNullOrEmpty(book1.getType()), Book::getType, book1.getType())
                .or().like(!Strings.isNullOrEmpty(book1.getDescription()), Book::getDescription, book1.getDescription());
        IPage<Book> selectedPage = bookMapper.selectPage(iPage, wrapper);
        return selectedPage;
    }

    /**
     * @param str
     * @return
     */
    @Override
    public ResultResponse modifyBooks(String str) {
        if(StringUtils.isEmpty(str)){
            return new ResultResponse(false,"参数为空");
        }
        String input = "0".equals(str) ? "view" : ("1".equals(str) ?"modify":" ");
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        switch (input) {
            case "view":
                List<Book> books = bookMapper.selectList(wrapper);
                return new ResultResponse(true, "view success", books);
            case "modify":
                bookMapper.selectList(wrapper).stream().forEach(book -> {
                    book.setName(book.getName().trim());
                    book.setType(book.getType().trim());
                    book.setDescription(book.getDescription().trim());
                    book.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    bookMapper.updateById(book);
                });
                return new ResultResponse(true, "modify success", bookMapper.selectList(wrapper));
            default:
                return new ResultResponse(false, "条件错误");
        }
    }

    public static void main(String[] args) {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}
